package com.glodon.jcloud.increment.shorturl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.glodon.jcloud.increment.shorturl.service.ShortUrlService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 * Create Date: 2022/12/13
 *
 * @version: V0.0.1-SNAPSHOT
 * @author: Terry.Li
 * @mail: lib-o@glodon.com
 */
@Slf4j
@RequestMapping
@Controller
public class ShortUrlController {

    @Resource
    private ShortUrlService shortUrlService;

    /**
     * 解析短链接，并实现 302 跳转.
     *
     * @param alias    the alias
     * @param response the response
     * @throws IOException the io exception
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation("解析短链接，并实现 302 跳转")
    @GetMapping("/{alias}")
    public void redirect(@PathVariable String alias, HttpServletResponse response) throws IOException {
        log.info(alias);
//        response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
//        response.setHeader("Location","http://www.baidu.com");
        String link = shortUrlService.getLink(alias);
        response.sendRedirect(link);
    }

}
